package com.wangtianxiang.sxp.dbbase.config;

import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.wangtianxiang.sxp.dbbase.config.db.SxpDbConfigProperties;
import com.wangtianxiang.sxp.dbbase.config.processor.SxpDbBaseBeanDefinitionRegistryPostProcessor;
import org.apache.ibatis.plugin.Interceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.List;
import java.util.Map;

@Configuration
@EnableTransactionManagement
@ConditionalOnProperty(name = SxpDbConfigConstants.ENABLED, matchIfMissing = true)
public class DbbaseAutoConfiguration {

	//key 为数据源配置key(key=*, 代表所有数据源均采用此配置，并忽略其他key下的配置)
	private static Map<String, GlobalConfig> globalConfigMap;

	//key 为数据源配置key(key=*, 代表所有数据源均采用此配置，并忽略其他key下的配置)
	private static Map<String, List<Interceptor>> interceptorsMap;

	@Bean
	@ConditionalOnMissingBean
	@ConfigurationProperties(prefix = SxpDbConfigConstants.CONFIG_PREFIX)
	public SxpDbConfigProperties sxpDbConfigProperties() {
		return new SxpDbConfigProperties();
	}

	@Bean
	@ConditionalOnMissingBean
	public static SxpDbBaseBeanDefinitionRegistryPostProcessor womuicDbBaseBeanDefinitionRegistryPostProcessor() {
//		return new WomuicDbBaseBeanDefinitionRegistryPostProcessor(SxpDbConfigConstants.CONFIG_PREFIX);
		return new SxpDbBaseBeanDefinitionRegistryPostProcessor(SxpDbConfigConstants.CONFIG_PREFIX, globalConfigMap, interceptorsMap);
	}

	// mybatis 增强配置
	public static void mybatisConfigEnhanced( Map<String, GlobalConfig> globalConfigMap, Map<String, List<Interceptor>> interceptorsMap){
		if(null == DbbaseAutoConfiguration.globalConfigMap ){
			DbbaseAutoConfiguration.globalConfigMap = globalConfigMap;
		}
		if(null == DbbaseAutoConfiguration.interceptorsMap ){
			DbbaseAutoConfiguration.interceptorsMap = interceptorsMap;
		}
	}
}
